package com.neuedu.his.controller;

import com.neuedu.his.po.Register;
import com.neuedu.his.po.User;
import com.neuedu.his.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    RegisterService service;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Register r, HttpSession session) {
        Map<String, Object> result = new HashMap<>();

        try {
            r.setRegisterid(1);
            r.setRegisttime(new Date());
            r.setVisitstate(1);

            boolean ok = service.save(r);
            result.put("success", ok);
            result.put("message", ok ? "挂号成功" : "数据库保存失败");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "异常：" + e.getMessage());
        }

        return result;
    }


    @GetMapping("/doctor/{doctorId}/patients")
    public Map<String, List<Register>> getPatientsByDoctor(@PathVariable Integer doctorId) {
        List<Register> unvisited = service.getByDoctorAndVisitStates(doctorId, List.of(1, 3));
        List<Register> visited = service.getByDoctorAndVisitStates(doctorId, List.of(2));

        Map<String, List<Register>> result = new HashMap<>();
        result.put("unvisited", unvisited);
        result.put("visited", visited);
        return result;
    }

    @GetMapping("/department/{deptId}/patients")
    public Map<String, List<Register>> getPatientsByDepartment(@PathVariable Integer deptId) {
        List<Register> unvisited = service.getByDeptAndVisitStates(deptId, List.of(1, 3));
        List<Register> visited = service.getByDeptAndVisitStates(deptId, List.of(2));

        Map<String, List<Register>> result = new HashMap<>();
        result.put("unvisited", unvisited);
        result.put("visited", visited);
        return result;
    }


}



